/**
 * 
 */
package com.metacube.questionbank.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.metacube.questionbank.dao.UserDAO;
import com.metacube.questionbank.dao.VoteDAO;
import com.metacube.questionbank.model.Answer;
import com.metacube.questionbank.model.Question;

import com.metacube.questionbank.model.User;
import com.metacube.questionbank.service.UserService;
import com.metacube.questionbank.utility.MailSending;

/**
 * @author Admin39
 *
 */
public class UserServiceImpl implements UserService {
	@Autowired
	UserDAO userdao;
	@Autowired
	VoteDAO voteDao;
/*
 * @param user 
 * @return boolean
 * Transfers  User object from 
 */
	public boolean insertUser(User user) {
		return userdao.insertUser(user);
	}

	public User updateDetails(User user, MultipartFile file) {
		return userdao.updateData(user, file);
	}

	public User updateDetails(User user) {
		return userdao.updateData(user);
	}

	public void forgotPasswordmail(String toaddress, String subject,
			String message) {
		MailSending mailSending = new MailSending();
		mailSending.sendMail(toaddress, subject, message, message, message);
	}

	public User authenticate(User user) {
		return userdao.fetchAllDetailsFromUser(user);

	}

	public String getPassword(String to_address) {
		return userdao.getpassword(to_address);
	}

	public User getImage() {
		return null;
	}

	public List<User> getUsers() {
		return userdao.getUsers();
	}

	public List<User> getMatchedUser(String userName) {

		return userdao.getMatchedUser(userName);
	}

	public List<User> getUserQId(List<Question> questions) {
		List<User> userList = new ArrayList<User>();
		for (int qCounter = 0; qCounter < questions.size(); qCounter++) {
			userList.add(userdao.getUserByQId(questions.get(qCounter)
					.getUserId()));
		}
		return userList;
	}
	
	public User getUserQId(Question question) {
		User user=null;
		user=userdao.getUserByQId(question.getUserId());
		return user;
	}
	public List<User> getUserByAId(List<Answer> questionAnswers) {
		List<User> userList = new ArrayList<User>();
		for (int qCounter = 0; qCounter < questionAnswers.size(); qCounter++) {
			userList.add(userdao.getUserByAId(questionAnswers.get(qCounter)
					.getUserId()));
		}
		return userList;
	}

	public User getUserById(int userId) {
		return userdao.getUserbyId(userId);
	}

	public int getCountUpvotesForUserId(int userId) {
		int count = voteDao.getCountUpvotesForUser(userId);
		return count;

	}

	public int getCountDownUpvotesForUserId(int userId) {
		int count = voteDao.getCountDownUpvotesForUser(userId);
		return count;

	}

	public void setUserRating(int userId) {
		// TODO Auto-generated method stub
		int upvotes = getCountUpvotesForUserId(userId);
		int downvotes = getCountDownUpvotesForUserId(userId);
		int rating = (2 * upvotes) - downvotes;
		userdao.insertRating(rating, userId);

	}
}
